package com.UBC417.termproject.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.UBC417.termproject.Data.Auction;
import com.UBC417.termproject.Data.Bid;
import com.UBC417.termproject.Data.Item;
import com.UBC417.termproject.Data.Seller;
import com.UBC417.termproject.Data.User;

@SuppressWarnings("serial")
public class ViewAllAuctionsServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(SubscribeServlet.class.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		
		HttpSession session = req.getSession();
		String userId = (String) session.getAttribute("id");
		
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out= resp.getWriter();
		
		List<Auction> entries = Seller.ViewAllAuction(userId);
		
		if (entries.size()==0){
			//no results
			out.println("NO AUCTIONS!");
		}else{
			resp.getWriter().println("<table border='1'>");
			out.println("<tr>");
			out.println("<td>Item Name</td>");
			//out.println("<td>Auction ID</td>");
			out.println("<td>Base Price</td>");
			out.println("<td>Highest Bid</td>");
			out.println("<td>Start Date</td>");
			out.println("<td>End Date</td>");
			out.println("<td>Status</td>");
			out.println("<td>View Bids</td>");
			out.println("</tr>");
			for (Auction e : entries){
				Bid b= User.getHighestBid(e.getId());
				Item i = User.getAuctionItem(e.getId());
				out.println("<tr>");
				out.println("<td>"+e.getName()+"</td>");
				//out.println("<td><a>"+e.getId()+"</a></td>");
				out.println("<td>"+i.getBasePrice()+"</td>");
				out.println("<td>"+b.getPrice()+"</td>");
				out.println("<td>"+e.getStartTime()+"</td>");
				out.println("<td>"+e.getEndTime()+"</td>");
				out.println("<td>"+e.getStatus()+"</td>");
				out.println("<td><input type='button' id2= '"+e.getId()+"' data-bp2='"+e.getName()+"' data-hp2='"+e.getEndTime()+"' onclick= 'monitor(this)' value='View'/input></td>");
				out.println("</tr>");
			}
			out.println("</table>");
		
		}
		
	}	


}
